<?php
header('Content-Type: text/html; charset=ISO-8859-1');
include_once getenv('DOCUMENT_ROOT').'/evento/Library/xajax/xajax_core/xajax.inc.php';
include_once getenv('DOCUMENT_ROOT').'/evento/VOB/VOBFactory.class.php';
include_once getenv('DOCUMENT_ROOT').'/evento/ACTION/ACTFactory.class.php';
include_once getenv('DOCUMENT_ROOT').'/evento/VIEW/template/TFormat.class.php';

if (!isset($_SESSION))
    session_start();

$VOBf = new VOBFactory();
$ACTf = new ACTFactory();
$f = new TFormat();

//objetos
$objSubitem = $VOBf->getSubitem();
$objActionSubitem = $ACTf->getSubitemAction();
$objUnidade = $VOBf->getUnidade();
$objUnidadeAction = $ACTf->getUnidadeAction();
$objItem = $VOBf->getItem();
$objItemAction = $ACTf->getItemAction();


if (isset($_GET['acao']) && $_GET['acao'] == 'addSubitem') {

$flag = true;
$key = $_POST['ide_subitem'].$_POST['ide_item'];

 if($objActionSubitem->verificarKeyExistente($key)){
        $flag = false;
    }
    if($flag){
          $objSubitem->setId($_POST['ide_subitem']);
          $objSubitem = $objActionSubitem->obterPorId($objSubitem);
          $objSubitem->setQuantidade($_POST['qtd_evento']);
          $objUnidade->setId($objSubitem->getIdUnidade());
          $objUnidade = $objUnidadeAction->obterPorId($objUnidade);
          $qtdUnd = $objSubitem->getQuantidade()."(".$objUnidade->getDescricao().")";
          $vlrQtd = $f->formatValorFinanceiro($objSubitem->getQuantidade() * $objSubitem->getValor());
          $objItem->setId($objSubitem->getIdItem());
          $objItem = $objItemAction->obterPorId($objItem);
          $idTable = "table".$objSubitem->getIdItem();

          $flagD = true;
           for ($iterator = $_SESSION['arrayObjSubitem']->getIterator(); $iterator->valid(); $iterator->next()) {
                if($iterator->current()->getIdItem() == $objSubitem->getIdItem()){
                    $flagD = false;
                }
            }
            $arrayReposta = array();
           if($flagD){
              //criando table Dinamica
              $arrayReposta['inserirNovo'] = "sim";
              $arrayReposta['subItem'] = $objSubitem->getNome();
              $arrayReposta['qtdUnd'] = $qtdUnd;
              $arrayReposta['vlrQtd'] = $vlrQtd;
              $arrayReposta['divKey'] = $objSubitem->getIdItem();
              $arrayReposta['tituloDiv'] = $objItem->getNome();
              $arrayReposta['idTable'] = $idTable;
              $arrayReposta['key'] = $key;
              $json = $f->json_encode($arrayReposta);
              echo $json;
              
          }else{
              $arrayAtr = array($objSubitem->getNome(),$qtdUnd,$vlrQtd);
              //$objResponse->gerarTabela($idTable, $key, $arrayAtr);
          }


          $objActionSubitem->addSubitem($objSubitem);
          //adicionando o valor
          $_SESSION['vlrTotalEvento'] +=  $objSubitem->getQuantidade() * $objSubitem->getValor();
          //$objResponse->assign("vlrTotalEvento", "innerHTML", $f->formatValorFinanceiro($_SESSION['vlrTotalEvento']));
    }


    if(!$flag){
        echo "{evento : '0' }";
    }



}//fim addSubitem



?>